package com.easy.dn.ui.main.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.easy.common.base.BaseActivity;
import com.easy.common.commonwidget.DnToolbar;
import com.easy.dn.bean.SearchResumeBean;
import com.easy.dn.ui.main.adapter.SearchResumeAdapter;
import com.easy.dn.ui.main.contract.SearchResumeContract;
import com.easy.dn.ui.main.model.SearchResumeModel;
import com.easy.dn.ui.main.presenter.SearchResumePresenter;
import com.easy.dn.utils.DnUtils;
import com.easy.dn.utils.MapHelper;
import com.easy.dn.widget.CustomLoadMoreView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import dn.easy.com.easyto.R;

/**
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * 单身狗就这样默默地看着你，一句话也不说。
 * <p>
 * 简历列表
 *
 * @author DN
 * @data 2017/8/1
 **/
public class SearchResumeActivity extends BaseActivity<SearchResumePresenter, SearchResumeModel> implements SearchResumeContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @Bind(R.id.toolbar)
    DnToolbar mToolbar;
    @Bind(R.id.rv_list)
    RecyclerView mRvList;
    @Bind(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Bind(R.id.cb_chinese)
    CheckBox mCbChinese;
    @Bind(R.id.cb_foreigner)
    CheckBox mCbForeigner;
    @Bind(R.id.cb_overseas_returnee)
    CheckBox mCbOverseasReturnee;
    private int page = 1, pageCount = 10;
    private List<SearchResumeBean.RowsBean> mData = new ArrayList<>();
    private SearchResumeAdapter mAdapter;
    private boolean mLoadMoreEndGone = false;
    private CustomLoadMoreView mLoadMoreView;
    // 关键字搜索 and 简易搜索
    private String keyWord = "", jobType = "", tradeType = "", city = "", userType = "",
            property = "", education = "", language = "", unsuited = "",
            salaryBegin = "", salaryEnd = "", arrivalTimeBegin = "",
            arrivalTimeEnd = "", expTimeBegin = "", expTimeEnd = "",
            workHistory = "", introduction = "";
    private Map<String, String> mMap;
    private boolean isRefresh = true;
    private List<String> mUserTypeList;

    @Override
    public int getLayoutId() {
        return R.layout.act_search_resume;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        initTitle();
        mToolbar.setMainTitle(R.string.resume_list);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.rgb(147, 147, 147));
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SearchResumeAdapter(R.layout.item_search_resume, mData);
        mLoadMoreView = new CustomLoadMoreView();
        mAdapter.setLoadMoreView(mLoadMoreView);
        mAdapter.setOnLoadMoreListener(SearchResumeActivity.this, mRvList);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mLoadMoreEndGone = true;
        mRvList.setAdapter(mAdapter);
        loadData();
        mPresenter.getData(mMap);
        mUserTypeList = new ArrayList<>();

        /**
         * title 下面筛选华人 外国人 归国华人 设置
         * start-->
         */
        mCbChinese.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    mUserTypeList.add("0");
                }else {
                    mUserTypeList.remove("0");
                }
                selecterLoad();
            }
        });
        mCbForeigner.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    mUserTypeList.add("1");
                }else {
                    mUserTypeList.remove("1");
                }
                selecterLoad();
            }
        });
        mCbOverseasReturnee.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    mUserTypeList.add("2");
                }else {
                    mUserTypeList.remove("2");
                }
                selecterLoad();
            }
        });
        /**
         * title 下面筛选华人 外国人 归国华人 设置
         *  --> end
         */

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                List<SearchResumeBean.RowsBean > rowsBeanList = adapter.getData();
                bundle.putString("resumeID", rowsBeanList.get(position).getId()+"");
                bundle.putInt("type", rowsBeanList.get(position).getType());
                startActivity(ResumeDetailActivity.class,bundle);
//                ResumeDetailActivity.startAction(mContext,(ImageView)view.findViewById(R.id.iv_search_resume_headImg));
            }
        });
    }

    /**
     *  筛选 华人 外国人 归国华人 的请求调用方法
     */
    private void selecterLoad() {
        userType = "";
        StringBuffer stringBuffer = new StringBuffer();
        for (String user:mUserTypeList){
            stringBuffer.append(user+",");
        }
        userType = stringBuffer.toString();
        loadData();
        mPresenter.getData(mMap);
    }
    private void loadData() {
        mMap = MapHelper.getAccessMap();
        mMap.put("keyword", keyWord);
        mMap.put("jobType", jobType);
        mMap.put("tradeType", tradeType);
        mMap.put("city", city);
        mMap.put("address", "");
        mMap.put("userType", userType);
        mMap.put("property", property);
        mMap.put("education", education);
        mMap.put("language", language);
        mMap.put("unsuited", unsuited);
        mMap.put("salaryBegin", salaryBegin);
        mMap.put("salaryEnd", salaryEnd);
        mMap.put("arrivalTimeBegin", arrivalTimeBegin);
        mMap.put("arrivalTimeEnd", arrivalTimeEnd);
        mMap.put("expTimeBegin", expTimeBegin);
        mMap.put("expTimeEnd", expTimeEnd);
        mMap.put("workHistory", workHistory);
        mMap.put("introduction", introduction);
        mMap.put("pageNum", page + "");
        mMap.put("rowCountOfOnePage", pageCount + "");
        mMap.put("condition", "");
    }

    @Override
    public void onRefresh() {
        page = 1;
        isRefresh = true;
        loadData();
        mPresenter.getLoadOrRefreshData(mMap);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        isRefresh = false;
        loadData();
        mPresenter.getLoadOrRefreshData(mMap);
    }

    // 列表数据
    @Override
    public void returnResumeList(SearchResumeBean resumeList) {
        mData.clear();
        mData = resumeList.getRows();
        if (mData.size() == 0) {
            mAdapter.setEmptyView(DnUtils.getEmptyView(mContext));
        }
        if (mData.size() < pageCount) {
            mLoadMoreEndGone = true;
            mAdapter.loadMoreEnd(mLoadMoreEndGone);
        }
        mAdapter.setNewData(mData);
    }
    // 下拉 上拉 数据
    @Override
    public void returnLoadOrRefreshResumeList(SearchResumeBean resumeBean) {
        if (isRefresh) {
            mSwipeLayout.setRefreshing(false);
            mAdapter.setNewData(resumeBean.getRows());
        } else {
            mAdapter.loadMoreComplete();
            if (resumeBean.getRows().size() < pageCount) {
                mLoadMoreEndGone = true;
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            }
            mAdapter.addData(resumeBean.getRows());
        }
    }

    @Override
    public void showLoading(String title) {
    }

    @Override
    public void stopLoading() {
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
    }

}
